﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Shopping_Stall_ProductDetail_temp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["IndexTag"] != "" && Request.QueryString["IndexTag"] != null)
                hIndexTag.Value = Request.QueryString["IndexTag"];
            BindData();

        }
    }

    private void BindQuantityData()
    {
        DataTable dtBasket = new DataTable();

        if (Session["dtBasket"] != null)
        {
            dtBasket = (DataTable)Session["dtBasket"];
        }
        else
        {
            dtBasket.Columns.Add("PDID");
            dtBasket.Columns.Add("PDNAME");
            dtBasket.Columns.Add("PRICE");
            dtBasket.Columns.Add("QUANTITY");
            dtBasket.Columns.Add("RATE");
            dtBasket.Columns.Add("KINDNO");
        }
        if (txtQuantity.Text.Length <= 0)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Nhập vào số lượng')</script>");
            return;
        }
        clsCommon clsCom = new clsCommon();
        int index = clsCom.SearchDataTable(dtBasket, "PDID", Request.QueryString["PDID"]);
        if (index != -1) //Neu la loai san pham da ton tai thi cho phep so luong
        {
            if (lblKindNo.Text.Trim() == "P")
            {
                int QuantityList = int.Parse(dtBasket.Rows[index]["QUANTITY"].ToString());
                dtBasket.Rows[index]["QUANTITY"] = QuantityList + int.Parse(txtQuantity.Text);
            }
        }
        else
        {
            DataRow row = dtBasket.NewRow();

            string Price = lblDongia.Text;
            string Rate = lblRate.Text;
            if (ViewState["IsSaleOf"].ToString() == "Y")
            {
                Price = lblDiscountPrice.Text;
                Rate = lblDiscountRate.Text;
            }
            row["PDID"] = Request.QueryString["PDID"];
            row["PDNAME"] = lblTenSP.Text;
            row["PRICE"] = Price;
            row["QUANTITY"] = txtQuantity.Text;
            row["RATE"] = Rate;
            row["KINDNO"] = lblKindNo.Text;
            dtBasket.Rows.Add(row);

        }
        Session["dtBasket"] = dtBasket;
        Response.Redirect("../Basket/Basket.aspx");

    }


    private void BindData()
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("PdID", cls.killSQLInjection(Request.QueryString["PDID"]));
        DataTable dt = cls.Get_ProductByProductID(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
        {
            cls.UpdateViewCount(cls.killSQLInjection((Request.QueryString["PDID"])));
            lblKind.Text = "<a href='Stall.aspx?TopicID=" + dt.Rows[0]["TopicID"].ToString() + "' style='color:white;text-decoration:none'>" + dt.Rows[0]["TopicName"].ToString() + "</a>-<a href='Stall.aspx?CatID=" + dt.Rows[0]["CatID"].ToString() + "' style='color:white;text-decoration:none'>" + dt.Rows[0]["CatName"].ToString() + "</a>";
            lblTenSP.Text = dt.Rows[0]["PDName"].ToString();
            lblWarranty.Text = dt.Rows[0]["Warranty"].ToString();
            lblDongia.Text = FormatMoney(dt.Rows[0]["Price"].ToString());
            lblDescript.Text = dt.Rows[0]["Descript"].ToString();
            lblRate.Text = dt.Rows[0]["Rate"].ToString();
            lblKindNo.Text = dt.Rows[0]["KindNo"].ToString();
            //saleof price
            lblDiscountPrice.Text = dt.Rows[0]["DiscountPrice"].ToString();
            lblDiscountRate.Text = dt.Rows[0]["DiscountRate"].ToString();
            if (lblKindNo.Text != "P")
            {
                txtQuantity.ReadOnly = true;
                txtQuantity.CssClass = "txt_ReadOnly";
                txtQuantity.BackColor = System.Drawing.Color.FromName("#eaeaea");
                trBaoHanh.Visible = false;
                trGiaThamKhao.Visible = false;
            }
            //SaleOf
            ViewState["IsSaleOf"] = "N";
            if (dt.Rows[0]["IsSaleOf"].ToString() == "Y")
            {
                ViewState["IsSaleOf"] = "Y";
                trSaleOf.Visible = true;
                lblDiscountPrice.Text = FormatMoney(lblDiscountPrice.Text);
                tdPrice.Attributes.Add("class", "td_linethrought");
            }
            if (dt.Rows[0]["Status"].ToString() == "C")
            {
                tdPrice.Attributes.Add("class", "td_linethrought");
            }


            if (lblDescript.Text.Trim() == "")
                lblDescript.Text = "Không có mô tả cho sản phẩm này";

            string ImgUrl = dt.Rows[0]["imgURL"].ToString();
            if (ImgUrl == "" || ImgUrl == null)
            {
                imgDetail.Src = "~/Images/Body/Noimages.gif";
                zoom1.HRef = "#";
            }
            else
            {
                imgDetail.Src = "~/" + ImgUrl;
                zoom1.HRef = "~/" + ImgUrl;
                zoom1.Attributes.Add("title", lblTenSP.Text);
            }

            Bindpd_PropertiesDetail(dt.Rows[0]["CatID"].ToString(), dt.Rows[0]["PDID"].ToString());

        }


        string PDID = cls.killSQLInjection((Request.QueryString["PDID"]));

        DataSet ds = new DataSet();
        string strSQL = "select count(*) CountItem from product_img where  pdid='" + PDID + "'   ";
        strSQL += "       SELECT count(*) CountItem   FROM [Product_Comment] where pdid='" + PDID + "'   ";
        strSQL += "        select count(*) CountItem from product where Status<>'D' and PdID<>'" + PDID + "' and catid=(select catid from product where pdid='" + PDID + "')     ";
        ds = cls.GetData(strSQL);

        lblCTagImg.Text = ds.Tables[0].Rows[0]["CountItem"].ToString();
        lblCTagComment.Text = ds.Tables[1].Rows[0]["CountItem"].ToString();
        string viewcount = System.Configuration.ConfigurationManager.AppSettings["SimilarPD"];
        if (int.Parse(ds.Tables[2].Rows[0]["CountItem"].ToString()) > int.Parse(viewcount))
            lblCTagPD.Text = viewcount;
        else
            lblCTagPD.Text = ds.Tables[2].Rows[0]["CountItem"].ToString();





    }

    private void Bindpd_PropertiesDetail(string CatID, string PDID)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("PDID", cls.killSQLInjection(PDID));
        msg.CreateFirstNode("PrtID", "0");
        msg.CreateFirstNode("CatID", cls.killSQLInjection(CatID));
        DataTable dt = new DataTable();

        dt = cls.Get_pd_PropertyDetailByCondition(msg.GetXmlStr.ToString()).Tables[0];

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[0]["PrtdDesc"].ToString() != "")
                    lblThuocTinh.Text += "- " + dt.Rows[i]["PrtNm"].ToString() + ": " + dt.Rows[i]["PrtdDesc"].ToString() + "<br>";
            }
        }
        else
        {
            lblThuocTinh.Text = "Không có thông số kỹ thuật cho sản phẩm này";
        }

    }
    protected void ImgGioHang_Click(object sender, ImageClickEventArgs e)
    {
        BindQuantityData();

    }

    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }


    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }

}
